﻿Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports System.Data
Partial Class FrontEnd_Control_Home_HotProducts
    Inherits System.Web.UI.UserControl

#Region "Sub and function"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then '
            LoadData()
        End If
    End Sub
    Private Sub LoadData()
        Using Data As New VSEntities
            Dim p = (From q In Data.Products Where q.IsSellOff = True And q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) Order By q.PublishedDate Descending
                     Select New With {q.ProductId, q.Title, q.Description, q.QueryString, q.ImagePreview, q.SellPrice, q.OriginalPrice, .CateQuerystring = q.Category.QueryString, q.CateID}).Take(10).ToList
            rptShow.DataSource = p
            rptShow.DataBind()

        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub rptShow_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptShow.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)
        Dim img As Image = CType(e.Item.FindControl("img"), Image)
        Dim ltrTitle As Literal = CType(e.Item.FindControl("ltrTitle"), Literal)
        Dim hplImg As HyperLink = CType(e.Item.FindControl("hplImg"), HyperLink)
        Dim lblSellPrice As Literal = CType(e.Item.FindControl("lblSellPrice"), Literal)
        lblSellPrice.Text = FormatNumber(row.SellPrice.ToString)
        Dim ltrOriginalPrice As Literal = CType(e.Item.FindControl("ltrOriginalPrice"), Literal)
        ltrOriginalPrice.Text = FormatNumber(row.OriginalPrice.ToString) & " đ"
        img.ImageUrl = CookedImageURL("210-280", row.ImagePreview.ToString)
        img.AlternateText = row.Title
        img.Style.Add("overflow", "hidden")
        Dim ltrDiscount As Label = CType(e.Item.FindControl("ltrDiscount"), Label)

        ltrDiscount.Text = CType((((row.OriginalPrice - row.SellPrice) / row.OriginalPrice) * 100), Integer).ToString("#,##0")

        If ltrDiscount.Text = "0" Then
            ltrDiscount.CssClass = "blind"
            ltrOriginalPrice.Text = ""
        End If
        ltrTitle.Text = row.Title
        hplImg.NavigateUrl = MainDomain & row.QueryString & "-" & row.ProductId.ToString & ".html"

    End Sub
#End Region
End Class
